//
//  YBReleaseMeetingViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/12/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBReleaseMeetingViewController.h"
#import "YBMeetingReleaseUnReadTableViewCell.h"
#define UNREADCELL @"YBMeetingReleaseUnReadTableViewCell"

#import "YBMeetingReleaseAllReadTableViewCell.h"
#define ALLREADCELL @"YBMeetingReleaseAllReadTableViewCell"

#import "YBMeetingReleaseHistoryTableViewCell.h"
#define HISTORYCELL @"YBMeetingReleaseHistoryTableViewCell"

#import "YBMeetingReleaseViewController.h"
#import "YBMeetingDetailViewController.h"
#import "LCustomAlertView.h"

#import "YBReleaseMeetingNoticeViewController.h"

@interface YBReleaseMeetingViewController ()<UITableViewDelegate , UITableViewDataSource , LCustomAlertDelegate>
@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , strong) UIButton * addButton;

@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;

@property (nonatomic , strong) NSMutableArray<YBMeetingReleaseModel *> * dataArray;

@property (nonatomic , strong) NSMutableArray<YBMeetingReleaseModel *> * historyArray;

@property (strong, nonatomic) JCAlertView  *jcAlertView;

@property (nonatomic , strong) YBMeetingReleaseModel * currentDeleteModel;
@end

@implementation YBReleaseMeetingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentDeleteModel = nil;
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self ybInitUserInterface];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}


-(void)ybInitUserInterface{
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.addButton];
}


#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    [YBRequestManager request_releaseMeetingListWithPage:[NSString stringWithFormat:@"%ld",self.page] successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array1 = [object objectForKey:@"FB_HYS"];
            NSArray *array2 = [object objectForKey:@"FB_HYS_ED"];
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[YBMeetingReleaseModel class] json:array1] mutableCopy] ;
            weakSelf.historyArray = [[NSArray yy_modelArrayWithClass:[YBMeetingReleaseModel class] json:array2] mutableCopy] ;
            
            if (weakSelf.dataArray.count == 0 && weakSelf.historyArray.count == 0) {
                [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:self.tableView Type:1];
            }
            [weakSelf.tableView reloadData];
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
    } failBlock:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:weakSelf.view];
    }];
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    [YBRequestManager request_releaseMeetingListWithPage:[NSString stringWithFormat:@"%ld",self.page] successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array1 = [object objectForKey:@"FB_HYS"];
            NSArray *array2 = [object objectForKey:@"FB_HYS_ED"];
            
            [weakSelf.dataArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[YBMeetingReleaseModel class] json:array1]] ;
            [weakSelf.historyArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[YBMeetingReleaseModel class] json:array2]] ;
            
            if (weakSelf.dataArray.count == 0 && weakSelf.historyArray.count == 0) {
                [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:self.tableView Type:1];
            }
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
        
    } failBlock:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
}



-(void)option_addButton:(UIButton *)sender{
    //发布按钮
    YBMeetingReleaseViewController * vc = [[YBMeetingReleaseViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)option_noticeButton:(UIButton *)sender{
    //短信提醒按钮
    NSInteger index = sender.tag - 10000;
    YBMeetingReleaseModel * model = self.dataArray[index];
    YBReleaseMeetingNoticeViewController * vc = [[YBReleaseMeetingNoticeViewController alloc]init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)option_cellDeleteButton:(UIButton *)sender{
    NSInteger index = sender.tag - 400;
    self.currentDeleteModel = self.historyArray[index];
    LCustomAlertView *alert = [[LCustomAlertView alloc] initWithFrame:self.view.bounds alertMessage:[NSString stringWithFormat:@"是否删除 %@?" , self.currentDeleteModel.TITLE] titlesArray:@[@"取消",@"确定"]];
    alert.delegate = self;
    self.jcAlertView = [[JCAlertView alloc]initWithCustomView:alert dismissWhenTouchedBackground:YES];
    [self.jcAlertView show];
}



-(void)option_returnButton:(UIButton *)sender{
    NSInteger index = sender.tag - 5000;
    self.currentDeleteModel = self.dataArray[index];
    LCustomAlertView *alert = [[LCustomAlertView alloc] initWithFrame:self.view.bounds alertMessage:[NSString stringWithFormat:@"是否撤回 %@?\n可能已有人报名" , self.currentDeleteModel.TITLE] titlesArray:@[@"取消",@"确定"]];
    alert.delegate = self;
    self.jcAlertView = [[JCAlertView alloc]initWithCustomView:alert dismissWhenTouchedBackground:YES];
    [self.jcAlertView show];
}

#pragma mark - LCustomAlertDelegate
- (void)lCustomAlertWithView:(LCustomAlertView *)view index:(NSInteger)index{
    
    [self.jcAlertView dismissWithCompletion:^{
        if (index == 1) {
            /**  确认  */
            if ([self.currentDeleteModel.IS_PAST integerValue] == 1) {
                [YJProgressHUD showMessage:@"删除中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
                [YBRequestManager request_deleteMeetingWithHyId:self.currentDeleteModel.ID successBlock:^(id object) {
                    [YJProgressHUD hide];
                    NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
                    if ([model.STATE isEqualToString:@"20000"]) {
                        [YJProgressHUD showSuccess:@"删除成功" inview:self.view];
                        [self.tableView.mj_header beginRefreshing];
                    }
                    else{
                        [YJProgressHUD showSuccess:@"删除失败" inview:self.view];
                    }
                } failBlock:^(id object) {
                    [YJProgressHUD hide];
                    [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
                }];
            }
            else{
                [YJProgressHUD showMessage:@"撤回中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
                [YBRequestManager request_returnMeetingWithHyId:self.currentDeleteModel.ID successBlock:^(id object) {
                    [YJProgressHUD hide];
                    NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
                    if ([model.STATE isEqualToString:@"20000"]) {
                        [YJProgressHUD showSuccess:@"撤回成功" inview:self.view];
                        [self.tableView.mj_header beginRefreshing];
                    }
                    else{
                        [YJProgressHUD showSuccess:@"撤回失败" inview:self.view];
                    }
                } failBlock:^(id object) {
                    [YJProgressHUD hide];
                    [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
                }];
            }
            
        }
        self.currentDeleteModel = nil;
    }];
}




-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.historyArray.count == 0) {
        return 1;
    }
    else{
        return 2;
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.dataArray.count;
    }
    else{
        return self.historyArray.count;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (self.dataArray[indexPath.row].NO_ENROLLS.count > 0) {
            return ItemHeight * 240 + self.dataArray[indexPath.row].unReadMsgHeight;
        }
        else{
            return ItemHeight * 140;
        }
    }
    else{
        return ItemHeight * 140;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (self.dataArray[indexPath.row].NO_ENROLLS.count > 0) {
            YBMeetingReleaseUnReadTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:UNREADCELL forIndexPath:indexPath];
            cell.model = self.dataArray[indexPath.row];
            cell.deleteButton.tag = 5000 + indexPath.row;
            [cell.deleteButton addTarget:self action:@selector(option_returnButton:) forControlEvents:UIControlEventTouchUpInside];
            cell.noticeButton.tag = 10000 + indexPath.row;
            [cell.noticeButton addTarget:self action:@selector(option_noticeButton:) forControlEvents:UIControlEventTouchUpInside];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        else{
            YBMeetingReleaseAllReadTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ALLREADCELL forIndexPath:indexPath];
            cell.model = self.dataArray[indexPath.row];
            cell.deleteButton.tag = 5000 + indexPath.row;
            [cell.deleteButton addTarget:self action:@selector(option_returnButton:) forControlEvents:UIControlEventTouchUpInside];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    else{
        YBMeetingReleaseHistoryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:HISTORYCELL forIndexPath:indexPath];
        cell.model = self.historyArray[indexPath.row];
        cell.deleteButton.tag = 400 + indexPath.row;
        [cell.deleteButton addTarget:self action:@selector(option_cellDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    else{
        return ItemHeight * 80;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    }
    else{
        UITableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
        if (!headerView) {
            headerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"header"];
            UILabel * headLabel = [ToolClass creatLabelWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], ItemHeight * 80) font:15 textColor:[UIColor textGrayColor] text:@"历史消息"];
            headLabel.backgroundColor = [UIColor themeGrayColor];
            headLabel.textAlignment = NSTextAlignmentCenter;
            [headerView addSubview:headLabel];
        }
        return headerView;
    }
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YBMeetingReleaseModel * model;
    if (indexPath.section == 0) {
        model = self.dataArray[indexPath.row];
        YBMeetingDetailViewController * vc = [[YBMeetingDetailViewController alloc]init];
        vc.model = model;
        vc.isSignIn = NO;
        vc.isShowButton = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else{
        model = self.historyArray[indexPath.row];
        YBMeetingDetailViewController * vc = [[YBMeetingDetailViewController alloc]init];
        vc.model = model;
        vc.isSignIn = NO;
        vc.isShowButton = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}



-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - 50 * [UIScreen zj_screenHScale] - navHeight - TabbarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[YBMeetingReleaseUnReadTableViewCell class] forCellReuseIdentifier:UNREADCELL];
        [_tableView registerClass:[YBMeetingReleaseAllReadTableViewCell class] forCellReuseIdentifier:ALLREADCELL];
        [_tableView registerClass:[YBMeetingReleaseHistoryTableViewCell class] forCellReuseIdentifier:HISTORYCELL];
        _tableView.delegate =self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        [_tableView adaptiveIOS11];
    }
    return _tableView;
}


-(UIButton *)addButton
{
    if (!_addButton) {
        _addButton = [ToolClass createButtonWithFrame:CGRectMake([UIScreen zj_screenWith] - ItemHeight * 170, [UIScreen zj_screenHeight] - 135 * [UIScreen zj_screenHScale] - navHeight - TabbarHeight, ItemHeight * 150, ItemHeight * 150)  text:@"发布" textColor:[UIColor whiteColor] imageName:@"" font:16];
        _addButton.backgroundColor = [UIColor themeColor];
        _addButton.clipsToBounds = YES;
        _addButton.layer.cornerRadius = ItemHeight * 75;
        [_addButton addTarget:self action:@selector(option_addButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}



-(NSMutableArray<YBMeetingReleaseModel *> *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(NSMutableArray<YBMeetingReleaseModel *> *)historyArray
{
    if (!_historyArray) {
        _historyArray = [NSMutableArray array];
    }
    return _historyArray;
}


- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}


@end
